package com.example.administrator.mybabymall.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.mybabymall.R;


/**
 * Created by Administrator on 2017/11/25 0025.
 */

public class MyToolbar extends Toolbar {
    private LayoutInflater layoutInflater;
    private TextView textView;
    private EditText editText;
    private ImageView imageView;
    private TextView textManager;
    private View view;
    public MyToolbar(Context context) {
        this(context,null);
    }

    public MyToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }


    @SuppressLint("RestrictedApi")
    public MyToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
//        搜索框前后距离10
        setContentInsetsRelative(10,10);
//        判断attrs是否为空
        if(attrs!=null){
//            绑定自定义attrs
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.CarAddSubView, defStyleAttr, 0);
////设置Title文字颜色
//            if (a.hasValue(R.styleable.CarAddSubView_car_titleTextColor)) {
//                if(textView!=null){
//                    textView.setTextColor(R.styleable.CarAddSubView_car_titleTextColor);
//                }
//            }
//            rightbutton显示
            final Drawable navIcon = a.getDrawable(R.styleable.CarAddSubView_car_rightButtonView);
            if (navIcon != null) {
//                setNavigationIcon(navIcon);
                setRightBottonViewIcon(navIcon);
            }
//            是否显示搜索框
            boolean isShowSearch = a.getBoolean(R.styleable.CarAddSubView_showSearchView,false);
            if(isShowSearch){
                showSearchView();
                hideTitleView();
            }
//            是否显示textmanager
            boolean isShowRightTextManager = a.getBoolean(R.styleable.CarAddSubView_car_rightTextManager,false);
            if(isShowRightTextManager){
                showtextManager();
//                对 textmanager进行监听
                setRightTextManagerClick(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getContext(),"click textmanager",Toast.LENGTH_SHORT).show();
                    }
                });
            }
//            回收TintTypedArray
            a.recycle();
        }

    }

//    初始化View，进行控件绑定
    private void initView() {
        if(view==null){
            layoutInflater = LayoutInflater.from(getContext());
            view = layoutInflater.inflate(R.layout.toolbar,null);

            textView = view.findViewById(R.id.toolbar_title);
            editText = view.findViewById(R.id.toolbar_search);
            imageView = view.findViewById(R.id.toolbar_rightbotton);
            textManager = view.findViewById(R.id.toolbar_text_manager);

            LayoutParams layout = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
            addView(view,layout);
        }

    }
//设置rightbutton的图标
    public void setRightBottonViewIcon(Drawable icon){
        if(imageView!=null){
            imageView.setImageDrawable(icon);
            showRightButtonView();
        }
    }
//设置RightButton的监听
    public void setRightBottonViewIconClick(OnClickListener listener){
        imageView.setOnClickListener(listener);
    }
    //设置RightTextManager的监听
    public void setRightTextManagerClick(OnClickListener listener){
        textManager.setOnClickListener(listener);
    }
//重写这只标题的方法
    @Override
    public void setTitle(int resId) {
        setTitle(getContext().getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        initView();
        if (textView!=null){
            textView.setText(title);
            showTitleView();
        }

    }
//搜索框显示控制
    public void showSearchView(){
        if(editText!=null){
            editText.setVisibility(VISIBLE);
        }
    }
    public void hideSearchView(){
        if (editText!=null)
            editText.setVisibility(GONE);
    }
//    标题显示控制
    public void showTitleView(){
        if(textView!=null){
            textView.setVisibility(VISIBLE);
        }
    }
    public void hideTitleView(){
        if (textView!=null)
            textView.setVisibility(GONE);
    }
//    rightbutton显示控制
    public void showRightButtonView(){
        if(textView!=null){
            imageView.setVisibility(VISIBLE);
        }
    }
    public void hideRightButtonView(){
        if (imageView!=null)
            imageView.setVisibility(GONE);
    }
//    textmanager显示控制
public void showtextManager(){
    if(textManager!=null){
        textManager.setVisibility(VISIBLE);
    }
}
    public void hidetextManager(){
        if (textManager!=null)
            textManager.setVisibility(GONE);
    }
}
